package com.example.controller;

import com.example.constant.sta;
import com.example.pojo.FilterOptionsSample;
import com.example.pojo.PageResult;
import com.example.pojo.Result;
import com.example.pojo.Sample;
import com.example.service.SampleService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 数字样本管理控制器
 */
@Slf4j
@RequestMapping("/samples")
@RestController
public class SampleController {

    @Autowired
    private SampleService sampleService;

    /**
     * 分页查询数字样  -GET http://localhost:8881/samples/sample?page=1&pageSize=10
     */
    @GetMapping("/sample")
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize){
        log.info("分页查询数字样，当前页码为：{}，每页记录数：{}", page, pageSize);
        PageResult pageResult = sampleService.page(page, pageSize);
        return Result.success(pageResult);
    }


    /**
     * 分页查询备选数字样  -GET http://localhost:8881/samples/sampleb?page=1&pageSize=10
     */
    @GetMapping("/sampleb")
    public Result pageb(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("分页查询备选数字样，当前页码为：{}，每页记录数：{}", page, pageSize);
        PageResult pageResult = sampleService.pageb(page, pageSize);
        return Result.success(pageResult);
    }

    /**
     * 分页查询设备源数字样  -GET //samples/samplem?page=1&pageSize=10
     */
    @GetMapping("/samplem")
    public Result pagem(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize){
        log.info("分页查询备选数字样，当前页码为：{}，每页记录数：{}", page, pageSize);
        PageResult pageResult = sampleService.pagem(page, pageSize);
        return Result.success(pageResult);
    }

    /**
     * 分页查询正常图片  -GET http://localhost:8881/samples/samplen?page=1&pageSize=10
     */
    @GetMapping("/samplen")
    public Result pageN(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("分页查询正常图片，当前页码为：{}，每页记录数：{}", page, pageSize);
        PageResult pageResult = sampleService.pagen(page, pageSize);
        return Result.success(pageResult);
    }



    /**
     * 删除数字样  -DELETE /samples?sampleID=1
     */
    @DeleteMapping
    public Result delete(Integer sampleID) {
        log.info("删除数字样，数字样ID为：{}", sampleID);
        sampleService.deleteById(sampleID);
        return Result.success();
    }

    /**
     * 修改备选数字样  -PUT /samples/sampleb
     */
    @PutMapping("/sampleb")
    public Result updateb(@RequestBody Sample sample) {
        log.info("修改备选数字样信息:{}", sample);
        sampleService.update(sample);
        return Result.success();
    }

    /**
     * 导入到下发数字样库 -PUT /samples/sample
     */
    @PutMapping("/sample")
    public Result update(@RequestBody Sample sample) {
        log.info("导入到下发数字样库数字样信息:{}", sample);
        sampleService.upload(sample);
        return Result.success();
    }



    /**
     * 分页查询数字样并根据条件筛选  -GET  http://localhost:8881/samples/sample/filter?page=1&pageSize=10&brands=xxx&defets=xxx&grades=xxx
     */
    @GetMapping("/sample/filter")
    public Result querySamples(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "15") Integer pageSize,
            @RequestParam(required = false) List<String> brands,
            @RequestParam(required = false) List<String> defects,
            @RequestParam(required = false) List<String> grades) {
        log.info("分页查询数字样并筛选，当前页码为：{}，每页记录数：{}，品牌名称：{}，缺陷名称：{}，缺陷等级：{}", page, pageSize, brands, defects, grades);
        PageResult pageResult = sampleService.pageWithFilter(page, pageSize, brands, defects, grades);
        //log.info("sta.fileList:{}", sta.fileList);
        return Result.success(pageResult);
    }

    /**
     * 分页查询备选数字样并根据条件筛选  -GET http://localhost:8881/samples/sampleb/filter?page=1&pageSize=10&brands=xxx&defets=xxx&grades=xxx&nps=xxx
     */
    @GetMapping("/sampleb/filter")
    public Result querySamplesb(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "15") Integer pageSize,
            @RequestParam(required = false) List<String> brands,
            @RequestParam(required = false) List<String> defects,
            @RequestParam(required = false) List<String> grades,
            @RequestParam(required = false) List<Double> nps) {
        log.info("分页查询数字样并筛选，当前页码为：{}，每页记录数：{}，品牌名称：{}，缺陷名称：{}，缺陷等级：{}", page, pageSize, brands, defects, grades,nps);
        PageResult pageResult = sampleService.pagebWithFilter(page, pageSize, brands, defects, grades,nps);
        return Result.success(pageResult);
    }

    /**
     * 向digitalsample表中插入数据  -POST /samples/sample
     */
    @PostMapping("/sample")
    public Result save(@RequestBody Sample sample) {
        log.info("新增数字样，数字样数据为：{}", sample);
        sampleService.save(sample);
        return Result.success();
    }

    /**
     * 向digitalsamplemachine表中插入数据  -POST /samples/samplem
     */
    @PostMapping("/samplem")
    public Result savem(@RequestBody Sample sample) {
        log.info("新增数字样，数字样数据为：{}", sample);
        sampleService.savem(sample);
        return Result.success();
    }


    /**
     * 从数字样库中获取所有不同的品牌名称、缺陷名称和缺陷等级  --GET http://localhost:8881/samples/sample/names
     */
    @GetMapping("/sample/names")
    public ResponseEntity<FilterOptionsSample> getDistinctFilters() {
        log.info("获取所有品牌名称、缺陷名称和缺陷等级");
        return ResponseEntity.ok(sampleService.getDistinctFilterOptions());
    }

    /**
     * 从备选数字样库中获取所有不同的品牌名称、缺陷名称、缺陷等级和推荐指数  --GET http://localhost:8881/samples/sampleb/names
     */
    @GetMapping("/sampleb/names")
    public ResponseEntity<FilterOptionsSample> getDistinctFiltersb() {
        log.info("获取所有品牌名称、缺陷名称和缺陷等级");
        return ResponseEntity.ok(sampleService.getDistinctFilterOptionsb());
    }

    // SampleController.java 新增

    /**
     * 获取设备源图库所有不同设备id，品牌和缺陷类型 - GET http://localhost:8881/samples/samplem/names
     */
    @GetMapping("/samplem/names")
    public ResponseEntity<FilterOptionsSample> getDistinctFiltersm() {
        log.info("获取设备源图库的设备id，品牌和缺陷类型");
        return ResponseEntity.ok(sampleService.getDistinctFilterOptionsm());
    }

    /**
     * 获取正常图片所有设备id，品牌名称 - GET  http://localhost:8881/samples/samplen/names
     */
    @GetMapping("/samplen/names")
    public ResponseEntity<FilterOptionsSample> getDistinctFiltersn() {
        log.info("获取正常图片的品牌名称");
        return ResponseEntity.ok(sampleService.getDistinctFilterOptionsn());
    }


    /**
     * 分页筛选设备源图片 -GET  http://localhost:8881/samples/samplem/filter?page=1&pageSize=10&machineIDs=xxx&brands=xxx&defects=xxx&start=xxx&end=xxx
     */
    @GetMapping("/samplem/filter")
    public Result filterMachineSamples(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) List<String> machineIDs,
            @RequestParam(required = false) List<String> brands,
            @RequestParam(required = false) List<String> defects,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date start,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date end) {
        log.info("设备源筛选：页码{} 页大小{} 设备{} 品牌{} 缺陷{} 时间[{}-{}]",
                page, pageSize, machineIDs,brands, defects, start, end);
        PageResult result = sampleService.pagemWithFilter(page, pageSize, machineIDs,brands, defects, start, end);
        return Result.success(result);
    }

    /**
     * 分页筛选正常图片 -GET   http://localhost:8881/samples/samplen/filter?page=1&pageSize=10&machineIDs=xxx&brands=xxx&start=xxx&end=xxx
     */
    @GetMapping("/samplen/filter")
    public Result filterNormalSamples(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) List<String> machineIDs,
            @RequestParam(required = false) List<String> brands,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date start,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date end) {
        log.info("正常图片筛选：页码{} 页大小{} 设备{} 品牌{} 时间[{}-{}]",
                page, pageSize, machineIDs, brands, start, end);
        PageResult result = sampleService.pagenWithFilter(page, pageSize, machineIDs, brands, start, end);
        return Result.success(result);
    }
}
